﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace MaskData
{
    class Text
    {
        /**************************************************************
         * OpenText() is the main function that spawns a new instance *
         * of notepad or whatever the host's default reader is. It is *
         * very important because our scanning functions rely on it   *
         * to start the process of actually reading our documents and *
         * then calling the necessary hash functions.                 *
         *************************************************************/

        // Create bool variable to determine how to cleanup.
        bool showSuccess = false;

        public void OpenText(string textPath)
        {
            // Create an instance to read the text file that was opened and assign the array.
            StreamReader sr = new StreamReader(textPath);

            // Call our function that kicks off the hashing.
            HashText(sr, textPath);
        }

        public string HashText(StreamReader er, string textPath)
        {
            // Instantiate a new instance of mdOptions so we
            // can access our objects.
            mdMain _mdMain = new mdMain();

            // If we match our expression, then we call the function to 
            // compute the hash, then write the hash back to the file that
            // was opened.
            // 
            // Read what the values are for the checkboxes first and assign them to a string.
            string _textEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "TextEnableHash", "Unchecked"));
            string _dataSSN = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "SSNHash", "Unchecked"));
            string _dataCC = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "CCHash", "Unchecked"));
            string _dataWells = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "WellsHash", "Unchecked"));
            string _dataBA = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "BAHash", "Unchecked"));

            // If Text is enabled, and everything is not checkmarked, then search for every type and hash.
            if (_textEnable == "Checked")
                if (_dataSSN == "Unchecked")
                    if (_dataCC == "Unchecked")
                        if (_dataWells == "Unchecked")
                            if (_dataBA == "Unchecked")
                            {
                                try
                                {
                                    String input;
                                    // Read and display lines from the file until the end of the file is reached.
                                    while ((input = er.ReadLine()) != null)
                                    {
                                        if (Regex.IsMatch(input, _mdMain.ssnExpressions))
                                        {
                                            string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                            StreamWriter sw = new StreamWriter("result.txt");
                                            sw.WriteLine(replaceBAHash);
                                            sw.Close();
                                            sw.Dispose();
                                            showSuccess = true;
                                        }

                                        if (Regex.IsMatch(input, _mdMain.ssnExpressions2))
                                        {
                                            string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                            StreamWriter sw = new StreamWriter("result.txt");
                                            sw.WriteLine(replaceBAHash);
                                            sw.Close();
                                            sw.Dispose();
                                            showSuccess = true;
                                        }

                                        if (Regex.IsMatch(input, _mdMain.ccExpressions))
                                        {
                                            string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                            StreamWriter sw = new StreamWriter("result.txt");
                                            sw.WriteLine(replaceBAHash);
                                            sw.Close();
                                            sw.Dispose();
                                            showSuccess = true;
                                        }

                                        if (Regex.IsMatch(input, _mdMain.wfBankAccountExpressions))
                                        {
                                            string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                            StreamWriter sw = new StreamWriter("result.txt");
                                            sw.WriteLine(replaceBAHash);
                                            sw.Close();
                                            sw.Dispose();
                                            showSuccess = true;
                                        }

                                        if (Regex.IsMatch(input, _mdMain.baBankAccountExpressions))
                                        {
                                            string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                            StreamWriter sw = new StreamWriter("result.txt");
                                            sw.WriteLine(replaceBAHash);
                                            sw.Close();
                                            sw.Dispose();
                                            showSuccess = true;
                                        }
                                    }
                                    er.Close();
                                    er.Dispose();
                                }
                                catch (UnauthorizedAccessException uae)
                                {
                                    // Write error logs to the application path.
                                    StreamWriter _writeLogs = new StreamWriter(_mdMain.AppPath);
                                    _writeLogs.WriteLine(uae);                                    
                                    _writeLogs.Close();
                                    _writeLogs.Dispose();

                                    MessageBox.Show(@"Unauthorized access has occurred.\nPlease restart the program
                                    and ensure that you have the correct permissions to run the program.\n\n
                                    This error has been logged at C:\Program Files\Mask Data\error.txt.");
                                }
                            }

            // If Text is enabled, AND ONLY the Social Security type is checkmarked
            // then find a match and hash.
            if (_textEnable == "Checked")
                if (_dataSSN == "Checked")
                {
                    try
                    {
                        String input;
                        // Read and display lines from the file until the end of the file is reached.
                        while ((input = er.ReadLine()) != null)
                        {
                            if (Regex.IsMatch(input, _mdMain.ssnExpressions))
                            {
                                string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                StreamWriter sw = new StreamWriter("result.txt");
                                sw.WriteLine(replaceBAHash);
                                sw.Close();
                                sw.Dispose();
                                showSuccess = true;
                            }

                            if (Regex.IsMatch(input, _mdMain.ssnExpressions2))
                            {
                                string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                StreamWriter sw = new StreamWriter("result.txt");
                                sw.WriteLine(replaceBAHash);
                                sw.Close();
                                sw.Dispose();
                                showSuccess = true;
                            }
                        }
                        er.Close();
                        er.Dispose();
                    }
                    catch (ArgumentNullException ane)
                    {
                        // Write error logs to the application path.
                        StreamWriter _writeLogs = new StreamWriter(_mdMain.AppPath);
                        _writeLogs.WriteLine(ane);
                        _writeLogs.Close();
                        _writeLogs.Dispose();                       
                    }
                }

            if (_textEnable == "Checked")
                if (_dataCC == "Checked")
                {
                    try
                    {
                        String input;
                        // Read and display lines from the file until the end of the file is reached.
                        while ((input = er.ReadLine()) != null)
                        {
                            if (Regex.IsMatch(input, _mdMain.ccExpressions))
                            {
                                string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                StreamWriter sw = new StreamWriter("result.txt");
                                sw.WriteLine(replaceBAHash);
                                sw.Close();
                                sw.Dispose();
                                showSuccess = true;
                            }
                        }
                        er.Close();
                        er.Dispose();
                    }
                    catch (Exception ee)
                    {
                        // Write error logs to the application path.
                        StreamWriter _writeLogs = new StreamWriter(_mdMain.AppPath);
                        _writeLogs.WriteLine(ee);
                        _writeLogs.Close();
                        _writeLogs.Dispose();

                        MessageBox.Show(@"An error with the program has occurred, however the program is still operational.\n
                        This error has been logged at C:\Program Files\Mask Data\error.txt.\n
                        Please send the full log to whughes05@gmail.com. Thank you.");
                    }
                }
            if (_textEnable == "Checked")
                if (_dataWells == "Checked")
                {
                    try
                    {
                        String input;
                        // Read and display lines from the file until the end of the file is reached.
                        while ((input = er.ReadLine()) != null)
                        {
                            if (Regex.IsMatch(input, _mdMain.wfBankAccountExpressions))
                            {
                                string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                StreamWriter sw = new StreamWriter("result.txt");
                                sw.WriteLine(replaceBAHash);
                                sw.Close();
                                sw.Dispose();
                                showSuccess = true;
                            }
                        }
                        er.Close();
                        er.Dispose();
                    }
                    catch (Exception ee)
                    {
                        // Write error logs to the application path.
                        StreamWriter _writeLogs = new StreamWriter(_mdMain.AppPath);
                        _writeLogs.WriteLine(ee);
                        _writeLogs.Close();
                        _writeLogs.Dispose();

                        MessageBox.Show(@"An error with the program has occurred, however the program is still operational.\n
                        This error has been logged at C:\Program Files\Mask Data\error.txt.\n
                        Please send the full log to whughes05@gmail.com. Thank you.");
                    }
                }
            if (_textEnable == "Checked")
                if (_dataBA == "Checked")
                {
                    try
                    {
                        String input;
                        // Read and display lines from the file until the end of the file is reached.
                        while ((input = er.ReadLine()) != null)
                        {
                            if (Regex.IsMatch(input, _mdMain.baBankAccountExpressions))
                            {
                                string replaceBAHash = SHA2Hash.ComputeHash(input, "SHA256", null);
                                StreamWriter sw = new StreamWriter("result.txt");
                                sw.WriteLine(replaceBAHash);
                                sw.Close();
                                sw.Dispose();
                                showSuccess = true;
                            }
                        }
                        er.Close();
                        er.Dispose();
                    }
                    catch (Exception ee)
                    {
                        // Write error logs to the application path.
                        StreamWriter _writeLogs = new StreamWriter(_mdMain.AppPath);
                        _writeLogs.WriteLine(ee);
                        _writeLogs.Close();
                        _writeLogs.Dispose();

                        MessageBox.Show(@"An error with the program has occurred, however the program is still operational.\n
                        This error has been logged at C:\Program Files\Mask Data\error.txt.\n
                        Please send the full log to whughes05@gmail.com. Thank you.");
                    }
                }

            // Report success.
            if (showSuccess == true)
            {
                Form newForm = new Form();
                newForm.TopMost = true;
                MessageBox.Show(newForm, "Fields masked successfully.", "Mask Data", MessageBoxButtons.OK);
                newForm.TopMost = false;
            }
            else
            {
                Form newForm = new Form();
                newForm.TopMost = true;
                MessageBox.Show(newForm, @"Mask Data either didn't find anything to mask or you didn't select an option on the main screen.", "Mask Data", MessageBoxButtons.OK);
                newForm.TopMost = false;
            }

            return textPath;
        }
    }
}